package GUI;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

import Socket.Client;

public class Login extends javax.swing.JFrame {
	
	private Client client;
	private static final long serialVersionUID = 5032644858165500809L;
	private Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); // Determine user's screen size
    int width = 300;
    int height = 400;
    
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField userid;
    private javax.swing.JPasswordField password;
    private javax.swing.JButton loginButton;
    private javax.swing.JButton registerButton;

    /** Creates new form NewAccount */
    public Login() {
     	try {
			client = new Client(this);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
    	initComponents();
    }
    
    private void initComponents()
    	{
    	setResizable (false);
    	setLocation (d.width / 2 - width / 2, d.height / 2 - height / 2);
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        userid = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JButton();
        registerButton = new javax.swing.JButton();
        
        Icon login = new ImageIcon("img/login.jpg");
        JLabel computer = new JLabel(login);
        Icon tabbed = new ImageIcon("img/admin.gif");
        JLabel tab = new JLabel(tabbed);

        getContentPane().setLayout(null);
        
        setTitle("miniDB");
        
        getContentPane().add(computer);
        computer.setBounds(10, 10, 273, 216);
        	
        //Admin Username label
        jLabel1.setForeground (Color.black);
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Username :");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 265, 75, 25);
        
        //Admin Password label
        jLabel2.setForeground (Color.black);
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Password :");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 300, 75, 25);

        getContentPane().add(userid);
        getContentPane().add(password);
        userid.setBounds(90, 265, 150, 25);        
        password.setBounds(90, 300, 150, 25);
        
        getContentPane().add(loginButton);
        getContentPane().add(registerButton);
        
        getContentPane().add(tab);
        tab.setBounds(10, 225, 280,110);
        
        //Register button
        registerButton.setText("REGISTER");
        registerButton.setFont(new java.awt.Font("Tahoma", 1, 9));
        registerButton.setBackground(Color.white);
        registerButton.setBounds(80, 342, 90, 25);
        registerButton.setToolTipText("Click to Register");
        registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				register();				
			}
        });
        //Login button
        loginButton.setText("LOGIN");
        loginButton.setFont(new java.awt.Font("Tahoma", 1, 9));
        loginButton.setBackground(Color.white);
        loginButton.setBounds (180, 342, 90, 25);
        loginButton.setToolTipText("Click to login");
		loginButton.addActionListener(new ActionListener(){
  			public void actionPerformed(ActionEvent evt)
			{
  				verifyLogin();
			}
        });
        pack();
        setIconImage (getToolkit().getImage ("img/logo_1.png"));
		setSize (width, height);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
    
   
    public static void main(String args[]) {
        new Login();
    }
    
    void register() {
    	String un = userid.getText().trim();
    	String pwd = new String(password.getPassword()).trim();
    	if (client.register(un, pwd))
    		JOptionPane.showMessageDialog(this, "Congradulations! Register Success!", "Success", JOptionPane.INFORMATION_MESSAGE);
    	else 
    		JOptionPane.showMessageDialog(this, "Register Failure", "Failure", JOptionPane.ERROR_MESSAGE);
    }

    void verifyLogin() {
		String un = userid.getText().trim();
		String pwd = new String(password.getPassword()).trim();
		if (client.login(un, pwd)) {
			new ClientFrame(client, un);
			dispose();
			return;
		}
		JOptionPane.showMessageDialog(this, "User name and password not match or user doesn't exist.", "Wrong Operation", JOptionPane.ERROR_MESSAGE);

	}
		
}